import request from './request'
import { ElLoading, ElMessageBox } from 'element-plus'

// 用户登录
export function login(data) {
  return request({
    url: '/login',
    method: 'post',
    data
  })
}
// 获取用户信息
export function getUserInfo(params) {
  return request({
    url: '/getUserInfo',
    method: 'get',
    params
  })
}

export function doGet(url, params) {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0, 0, 0, 0.7)',
  })

  try {
    const res = request({
      url,
      method: 'get',
      params
    })
    return res;
  } finally {
    loading.close()
  }
}

export async function doPost(url, data) {
  return new Promise((resolve) => {
    const loading = ElLoading.service({
      lock: true,
      text: 'Loading',
      background: 'rgba(0, 0, 0, 0.7)',
    });

    try {
      request({
        url,
        method: 'post',
        data
      }).then(response => {
        if (response.data.retCode === "000000") {
          resolve(response.data.data);
        }else{
          ElMessageBox.alert(response.data.retMsg, '', { center: true })
        }
      }).catch(error => {

        if (error.msg) {
          // 应用级 错误
          ElMessageBox.alert(error.msg, '', { center: true })
        } else if (error.response) {
          // 网络等其他错误
          ElMessageBox.alert(error.response.statusText, '', { center: true })
        }
        //reject(error);
      });
    } finally {
      loading.close();
    }
  });
}

export function downloadFile(url, data, downloadProgressCallback, obj) {
  return request({
    url,
    method: 'POST',
    data,
    responseType: 'blob',
    onDownloadProgress: function(progressEvent) {
      // 表示进度信息的总长度是否可以被计算
      if (progressEvent.lengthComputable) {
        if (downloadProgressCallback) {
          downloadProgressCallback(progressEvent, obj)
        }
      }
    }
  }).then(res => {
    const str = res.headers['content-disposition']
    if (!res || !str) {
      //error(res.message || '下载失败！')
      return
    }
    if (res && res.status === 200 && res.data) {
      const { data, headers } = res
      // // 切割出文件名
      // const fileNameEncode = res.headers['content-disposition'].split('filename=')[1]
      // // 解码
      // const fileName = decodeURIComponent(fileNameEncode)
      // console.log('fileName', fileName)
      let fileName
      if (headers['content-disposition']) {
        fileName = headers['content-disposition'].replace(/\w+;filename=(.*)/, '$1')
      } else {
        fileName = data.fileName
      }
      // 此处当返回json文件时需要先对data进行JSON.stringify处理，其他类型文件不用做处理
      const blob = new Blob([data], { type: headers['content-type'] })
      const dom = document.createElement('a')
      const downUrl = window.URL.createObjectURL(blob)
      dom.href = downUrl
      dom.download = decodeURIComponent(fileName)
      dom.style.display = 'none'
      document.body.appendChild(dom)
      dom.click()
      dom.parentNode.removeChild(dom)
      window.URL.revokeObjectURL(url)
    } else {
      //error(res.message || '下载失败！')
    }
  }).catch(error => {
      if (error.msg) {
        // 应用级 错误
        ElMessageBox.alert(error.msg, '', { center: true })
      } else if (error.response) {
        // 网络等其他错误
        ElMessageBox.alert(error.response.statusText, '', { center: true })
      }
    })
}


